<?php

namespace Home\Controller;

class AccountController extends \Home\Controller\ParentController {

    /**
     * @description 客栈用户
     * @modify songanwei 2016-03-17
     */
    public function index() {
        $where = $condition = array();
        $adminLoginModel = D('AdminLogin');
        $order = 'createtime desc';
        $count = $adminLoginModel->where($where)->order($order)->count();
        $Page = classPage($count);

        $adminLogins = $adminLoginModel->where($where)->order($order)->limit($Page->firstRow, $Page->listRows)->select();
		$page = $Page->show();
        $this->assign('adminLogins', $adminLogins);
		$this->assign('page', $page);
        $this->display();
    }

    /**
     * @description 客栈主账号管理
     * @modify songanwei 2016-03-17
     */
    public function main() {
        //查询数据
        $adminHotelModel = D('AdminHotel');

        $where['role'] = 1;
        $count = $adminHotelModel->where($where)->count('distinct adminlogin_id');

        $Page = classPage($count);
        $adminHotels = $adminHotelModel->where($where)->group('adminlogin_id')->limit($Page->firstRow, $Page->listRows)->select();
		$page = $Page->show();
        $adminLoginModel = D('AdminLogin');
        foreach ($adminHotels as &$adminHotel) {
            $adminLogin = $adminLoginModel->getInfo($adminHotel['adminlogin_id']);
            //
            $adminHotel['adminName'] = $adminLogin['name'];
            $adminHotel['adminMobile'] = $adminLogin['mobile'];
        }
        $this->assign('adminHotels', $adminHotels);
		$this->assign('page', $page);
        $this->display();
    }

    /**
     * @description 返回管理员客栈数据
     * @modify songanwei 2016-03-17
     */
    public function getHotel() {
        $userId = I('post.userId');
        //
        $adminHotelModel = D('AdminHotel');
        $adminHotels = $adminHotelModel->getInfosByAdmin($userId, 1);
        $data = array();
        //
        $hotelEntityModel = D('HotelEntity');
        foreach ($adminHotels as $adminHotel) {
            $data[] = $hotelEntityModel->getInfo($adminHotel['hotelentity_id']);
        }

        //转化数据
        foreach ($data as &$row) {
            $row['cityname'] = D('City')->getName($row['city_id']);
            $row['areaname'] = D('District')->getName($row['district_id']);
        }


        $this->returnJson(200, 200, $data);
    }
	/**
     * @description 返回管理员客栈数据
     * @modify zhangzhen 2016-12-28
     */
	public function shiyong() {
        $where = array(
			'endtime' => array('lt','2020-12-12 00:00:00'),
		);
        $adminLoginModel = D('AdminLogin');
        $order = 'createtime desc';
        $count = $adminLoginModel->where($where)->order($order)->count();
        $Page = classPage($count);
        $shiyongAccounts = $adminLoginModel->where($where)->order($order)->limit($Page->firstRow, $Page->listRows)->select();
		$page = $Page->show();
        $this->assign('shiyongAccounts', $shiyongAccounts);
		$this->assign('page', $page);
        $this->display();
    }
	/**
     * @description 设置试用时间
     * @modify zhangzhen 2016-12-28
     */
	public function setEndtime(){
		$params = I('post.');
		$adminId = $params['adminId'];
		$endtime = $params['endtime'] ? $params['endtime'] : '2020-01-01';
		
		$data = array(
			'endtime' => $endtime
		);
		
		$adminLoginModel = D('AdminLogin');
		$adminLoginModel->saveInfo($adminId, $data);
		$this->returnJson(200, 200,$data);
	}
	/**
     * @description 开通客栈
     * @modify zhangzhen 2016-12-28
     */
	public function setActive(){
		$params = I('post.');
		$adminId = $params['adminId'];
		$data = array(
			'endtime' => '2020-01-01',
			'status' => 3
		);
		$adminLoginModel = D('AdminLogin');
		$adminLoginModel->saveInfo($adminId, $data);
		$AdminHotelModel = D('AdminHotel');
		$AdminHotelDetail = $AdminHotelModel->getInfosByAdmin($adminId);
		foreach($AdminHotelDetail as $oneDetail){
			$hotel[] = $oneDetail['hotelentity_id'];
		}
		$hotelEntityModel = D('HotelEntity');
		$hotelEntityModel->setDate($hotel, '0000-00-00');
		
		$this->returnJson(200, 200,$params);
	}
}
